package com.xiaochong.tir.common.util;

import com.xiaochong.tir.common.data.hbasepo.DailyComprehensiveAnalysisInfo;

import java.time.LocalDateTime;
import java.util.*;

public class DailyInfoUtils {

    /**
     * 获取时间最接近的数据
     * @param dailyMap
     * @param hour
     * @param minute
     * @return
     */
    public static DailyComprehensiveAnalysisInfo getNearDailyInfo(
            Map<String, List<DailyComprehensiveAnalysisInfo>> dailyMap, int hour,int minute) {
        if(CollectionUtil.isBlank(dailyMap)){
            return null;
        }
        List<Integer> hours = new ArrayList<>();
        for (String key:dailyMap.keySet()) {
            hours.add(Integer.valueOf(key));
        }

        int minHourAbs = Integer.MAX_VALUE;
        for (Integer h:hours) {
            int abs = Math.abs(h - hour);
            if(abs<minHourAbs){
                minHourAbs = abs;
            }
        }
        List<Integer> minHours = new ArrayList<>();
        for (Integer h:hours) {
            int abs = Math.abs(h - hour);
            if(minHourAbs==abs){
                minHours.add(h);
            }
        }


        if(minHours.size()==1){
            int minSize = 0;
            List<DailyComprehensiveAnalysisInfo> dailynfos = dailyMap.get(minHours.get(0) + "");
            if(minHours.get(0) == hour){
                int minMinute = Integer.MAX_VALUE;
                for (int i = 0; i < dailynfos.size(); i++) {
                    int m = DateUtils.dateToLocalDateTime(dailynfos.get(i).getCreateTime()).getMinute();
                    int abs = Math.abs(m - minute);
                    if(abs<minMinute){
                        minMinute = abs;
                        minSize = i;
                    }
                }
            }else if(minHours.get(0)>hour){
                minSize = 0;
            }else if(minHours.get(0)<hour){
                minSize = dailynfos.size()-1;
            }
            return dailynfos.get(minSize);
        }else if(minHours.size()==2){
            int mainMinAbs = Integer.MAX_VALUE;
            DailyComprehensiveAnalysisInfo returnO = null;
            for (Integer minhour:minHours) {
                List<DailyComprehensiveAnalysisInfo> dailynfos = dailyMap.get(minhour + "");
                int minute1 = DateUtils.dateToLocalDateTime(dailynfos.get(0).getCreateTime()).getMinute();
                if(minhour>hour){
                    if(minute1<=mainMinAbs){
                        mainMinAbs = minute1;
                        returnO = dailynfos.get(0);
                    }
                }else if(minhour<hour){
                    if(minute1<mainMinAbs){
                        mainMinAbs = 59 - minute1;
                        returnO = dailynfos.get(dailynfos.size()-1);
                    }
                }
            }
            return returnO;
        }
        return null;

    }


    public static void main(String[] args){
        Map<String, List<DailyComprehensiveAnalysisInfo>> dailyMap = new HashMap<>();
        List<DailyComprehensiveAnalysisInfo> list1 = new ArrayList<>();
        DailyComprehensiveAnalysisInfo daily = new DailyComprehensiveAnalysisInfo();
        daily.setCreateTime(DateUtils.getDate("2018-05-12 09:59:00",DateUtils.format_ymdhms));
        list1.add(daily);
        dailyMap.put("9",list1);
        List<DailyComprehensiveAnalysisInfo> list2 = new ArrayList<>();
        daily = new DailyComprehensiveAnalysisInfo();
        daily.setCreateTime(DateUtils.getDate("2018-05-12 11:01:00",DateUtils.format_ymdhms));
        list2.add(daily);
        dailyMap.put("11",list2);
        DailyComprehensiveAnalysisInfo nearDailyInfo = DailyInfoUtils.getNearDailyInfo(dailyMap, 10, 30);
        System.out.println(nearDailyInfo);
    }

}
